package algorithm;

import conmon.BasicConstant;
import conmon.CommonUtil;

/**
 * @Description //TODO
 * @Author Administrator
 * @DATE 2020/11/1 17:47
 **/
public class Insert {
    public static void main(String[] args) {
        int[] arr = BasicConstant.ARR;
        insert(arr);
        CommonUtil.print(arr);
    }

    public static void insert(int[] arr) {
        int key, j;
        for (int i = 1; i < arr.length; i++) {
            //要插入的值
            key = arr[i];
            if (arr[i - 1] > key) {
                j = i;
                //从后往前比较，如果大于 key 值，则往后移动
                while (j > 0 && arr[j-1] > key){
                    //后往前移动
                    arr[j] = arr[j-1];
                    j--;
                }
                arr[j] = key;
            }
        }
    }
}
